/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name          : freertos.c
 * Description        : Code for freertos applications
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2025 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_system.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
    .name = "defaultTask",
    .stack_size = 128 * 4,
    .priority = (osPriority_t)osPriorityNormal,
};
/* Definitions for ml307r_RX_Queue */
osMessageQueueId_t ml307r_RX_QueueHandle;
const osMessageQueueAttr_t ml307r_RX_Queue_attributes = {
    .name = "ml307r_RX_Queue"};
/* Definitions for building_data_Queue */
osMessageQueueId_t building_data_QueueHandle;
const osMessageQueueAttr_t building_data_Queue_attributes = {
    .name = "building_data_Queue"};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
EventGroupHandle_t status_event_group;
TaskHandle_t ml307r_get_electricity_handle;
TaskHandle_t oled_ui_task_handle;

SemaphoreHandle_t ml307r_init_finish_sem;
void iwdg_task(void *argument);
/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
 * @brief  FreeRTOS initialization
 * @param  None
 * @retval None
 */
void MX_FREERTOS_Init(void)
{
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  ml307r_init_finish_sem = xSemaphoreCreateBinary();
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* creation of ml307r_RX_Queue */
  ml307r_RX_QueueHandle = osMessageQueueNew(5, sizeof(void *), &ml307r_RX_Queue_attributes);

  /* creation of building_data_Queue */
  building_data_QueueHandle = osMessageQueueNew(1, sizeof(void *), &building_data_Queue_attributes);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  xTaskCreate(ml307r_get_electricity, "get_electricity", 512, NULL, osPriorityNormal, &ml307r_get_electricity_handle);
  xTaskCreate(oled_ui_task, "oled_ui_task", 512, NULL, osPriorityNormal1, &oled_ui_task_handle);
  xTaskCreate(battery_task, "battery_task", 128, NULL, osPriorityNormal, NULL);
  // xTaskCreate(iwdg_task, "iwdg_task", 128, NULL, osPriorityNormal1, NULL);
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  status_event_group = xEventGroupCreate();

  /* USER CODE END RTOS_EVENTS */
}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
 * @brief  Function implementing the defaultTask thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for (;;)
  {
    debug_printf("hello freertos\r\n");
    osDelay(3000);
  }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void iwdg_task(void *argument)
{
  while (1)
  {
    // debug_printf("iwdg\r\n");
    HAL_IWDG_Refresh(&hiwdg);
    osDelay(800);
  }
}
/* USER CODE END Application */
